package com.wink_172

import android.database.sqlite.SQLiteOpenHelper
import android.content.Context
import android.database.sqlite.SQLiteDatabase
import com.wink_172.library.utils.CommonUtils

/**
p290
 */
class CustomerSQLiteOpenHelper(val context: Context, name: String, version: Int) :
        SQLiteOpenHelper(context, name, null, version) {
    //把数据库语句在数据库查看工具上运行
    private val createBook = "create table Book ( id integer primary key autoincrement, author text, price real, pages integer, name text) "
    private val createCategory = "create table Category( id integer primary key autoincrement, category_name text,category_code intger)"
    override fun onCreate(db: SQLiteDatabase?) {
        db?.execSQL(createBook)
//        db?.execSQL(createCategory)//newVersion>=2
        CommonUtils.showToast("Create table success")
    }

    override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) {
        if (oldVersion <= 1) {//newVersion>=2
            db?.execSQL(createCategory)
        }

        if (oldVersion <= 2) {//newVersion>=3
            db?.execSQL("alter table Book add column category_id intger")
        }
    }
}